package com.example.leetdemo.finash;

public class Solution1034 {

    /**
     * dps遍历上下左右
     * @param grid
     * @param row
     * @param col
     * @param color
     * @return
     */
    public static int[][] colorBorder(int[][] grid, int row, int col, int color) {
        int[][] check = new int[grid.length][grid[0].length];
        dps(grid, row, col, row, col, check);
        for (int i = 0; i < check.length; i++) {
            for (int j = 0; j < check[0].length; j++) {
                if (check[i][j] == 1) {
                    grid[i][j] = color;
                }
            }
        }
        return grid;
    }


    public static void dps(int[][] grid, int x, int y, int beginX, int beginY, int[][] check) {
        // 判断是否越界
        if (x < 0 || y < 0 || y >= grid[0].length || x >= grid.length) {
            return;
        }
        if (grid[x][y] == grid[beginX][beginY] && check[x][y] == 0) {
            if (x > 0 && x < grid.length - 1 && y > 0 && y < grid[0].length - 1) { // 边界
                // 是否十字相同
                if (grid[x][y] == grid[x][y - 1] && grid[x][y - 1] == grid[x - 1][y]
                        && grid[x + 1][y] == grid[x][y + 1]
                        && grid[x][y - 1] == grid[x][y + 1]) {
                    check[x][y] = 2;
                } else {
                    check[x][y] = 1;
                }
            } else { // 非边界
                check[x][y] = 1;
            }
            dps(grid, x - 1, y, beginX, beginY, check);
            dps(grid, x, y -1, beginX, beginY, check);
            dps(grid, x + 1, y, beginX, beginY, check);
            dps(grid, x , y + 1, beginX, beginY,check);
        }
    }

    public static void main1(String[] args) {
//        int[][] grid = new int[][] {{1,2,1,2,1,2}, {2,2,2,2,1,2}, {1,2,2,2,1,2}};
//        colorBorder(grid, 1, 1, 2);
        int[][] grid = new int[][] {{1,1,1}, {1,1,1}, {1,1,1}};
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                System.err.print(grid[i][j] + " ");
            }
            System.err.println();
        }
        System.err.println();
        colorBorder(grid, 1, 1, 2);
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                System.err.print(grid[i][j] + " ");
            }
            System.err.println();
        }
    }


    public static void test() {
        System.err.println(1111);
    }

    public static void main(String[] args) {
        Solution1034 solution1034 = new Solution1034();
        solution1034.test2();
    }

    public void test2() {
        System.err.println(2);
        test();
    }
}
